/*
 * Copyright (C) 2008 nextcode:ch
 * 
 * Author: Michele Dell'Ambrogio, m@nextcode.ch
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as 
 * published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package sfplayer.videoPlayer;

import java.awt.Component;
import java.util.logging.Level;
import java.util.logging.Logger;
import quicktime.QTException;
import quicktime.QTSession;
import quicktime.app.view.QTComponent;
import quicktime.app.view.QTFactory;
import quicktime.std.StdQTConstants;
import quicktime.std.StdQTException;
import quicktime.std.movies.Movie;
import quicktime.std.movies.MovieController;
import quicktime.std.movies.media.DataRef;

/**
 *
 * @author micc
 */
public class Quicktime implements VideoEngine {
    
    private Movie movie = null;
    private MovieController movieController = null;
    private QTComponent qtComponent = null;
    private Component videoComponent = null;

    public Quicktime() {
        try {
            QTSession.open();
        } catch (QTException ex) {
            Logger.getLogger(Quicktime.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void start() {
        if (movie != null) {
            try {
                movie.start();
            } catch (StdQTException ex) {
                Logger.getLogger(Quicktime.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void stop() {
        if (movie != null) {
            try {
                movie.stop();
            } catch (StdQTException ex) {
                Logger.getLogger(Quicktime.class.getName()).log(Level.SEVERE, null, ex);
            }
        }    
    }

    public void openUrl(String url) {
        try {
            
            DataRef dr = new DataRef(url);
            movie = Movie.fromDataRef(dr, StdQTConstants.newMovieActive);
            movieController = new MovieController(movie);

            qtComponent = QTFactory.makeQTComponent(movie);
            qtComponent.setMovieController(movieController);
            videoComponent = qtComponent.asComponent();

            movie.prePreroll(0, 1.0f);
            movie.preroll(0, 1.0f);
            
            movie.start();
            
        } catch (QTException ex) {
            Logger.getLogger(Quicktime.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Component getVideoComponent() {
        return videoComponent;
    }

    public void openFile(String file) {
        try {

            DataRef dr = new DataRef(file);
            movie = Movie.fromDataRef(dr, StdQTConstants.newMovieActive);
            movieController = new MovieController(movie);

            qtComponent = QTFactory.makeQTComponent(movie);
            qtComponent.setMovieController(movieController);
            videoComponent = qtComponent.asComponent();
            
            movie.start();
            
        } catch (QTException ex) {
            Logger.getLogger(Quicktime.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

}
